package com.youlai.boot.factory.controller;

import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.model.Option;
import com.youlai.boot.factory.service.ClothingCategoryService;
import com.youlai.boot.system.model.query.MenuQuery;
import com.youlai.boot.system.model.vo.MenuVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.ClothingCategoryForm;
import com.youlai.boot.factory.model.query.ClothingCategoryQuery;
import com.youlai.boot.factory.model.vo.ClothingCategoryVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 服装分类前端控制层
 *
 * @author youlaitech
 * @since 2025-08-16 00:26
 */
@Tag(name = "服装分类接口")
@RestController
@RequestMapping("/api/v1/clothing-category")
@RequiredArgsConstructor
public class ClothingCategoryController  {

    private final ClothingCategoryService clothingCategoryService;

    @Operation(summary = "服装分类树形列表")
    @GetMapping("/tree")
    @PreAuthorize("@ss.hasPerm('factory:clothing-category:query')")
    public Result<List<ClothingCategoryVO>> listTreeCategories(ClothingCategoryQuery queryParams) {
        List<ClothingCategoryVO> categoryVOList = clothingCategoryService.listTreeCategories(queryParams);
        return Result.success(categoryVOList);
    }

    @Operation(summary = "服装下拉列表")
    @GetMapping("/options")
    public Result<List<Option<Long>>> listOptions(
            @Parameter(description = "是否只查询父级菜单")
            @RequestParam(required = false, defaultValue = "false") boolean onlyParent
    ) {
        List<Option<Long>> categories = clothingCategoryService.listOptions(onlyParent);
        return Result.success(categories);
    }


    @Operation(summary = "服装分类分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:clothing-category:query')")
    public PageResult<ClothingCategoryVO> getClothingCategoryPage(ClothingCategoryQuery queryParams ) {
        IPage<ClothingCategoryVO> result = clothingCategoryService.getClothingCategoryPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增服装分类")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:clothing-category:add')")
    public Result<Void> saveClothingCategory(@RequestBody @Valid ClothingCategoryForm formData ) {
        boolean result = clothingCategoryService.saveClothingCategory(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取服装分类表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:clothing-category:edit')")
    public Result<ClothingCategoryForm> getClothingCategoryForm(
        @Parameter(description = "服装分类ID") @PathVariable Long id
    ) {
        ClothingCategoryForm formData = clothingCategoryService.getClothingCategoryFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改服装分类")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:clothing-category:edit')")
    public Result<Void> updateClothingCategory(
            @Parameter(description = "服装分类ID") @PathVariable Long id,
            @RequestBody @Validated ClothingCategoryForm formData
    ) {
        boolean result = clothingCategoryService.updateClothingCategory(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除服装分类")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:clothing-category:delete')")
    public Result<Void> deleteClothingCategorys(
        @Parameter(description = "服装分类ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = clothingCategoryService.deleteClothingCategorys(ids);
        return Result.judge(result);
    }
}
